package com.heima.wemedia.listener;

import com.alibaba.fastjson.JSON;
import com.heima.common.constant.GlobalConstant;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.wemedia.service.WmUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Slf4j
public class WemediaListener {
    @Autowired
    KafkaTemplate kafkaTemplate;
    @Autowired
    WmUserService wmUserService;
    @KafkaListener(topics = GlobalConstant.ADD_WM_USER)
    public void addWmUser(String msg)
    {
        if (StringUtils.isNotBlank(msg))
        {
            log.info("主题：{} ，收到一条信息：{}",GlobalConstant.ADD_WM_USER,msg);
            WmUser wmUser = JSON.parseObject(msg, WmUser.class);
            wmUser.setCreatedTime(new Date());
            wmUserService.save(wmUser);
            ApAuthor apAuthor = new ApAuthor();
            apAuthor.setUserId(wmUser.getApUserId());
            apAuthor.setWmUserId(wmUser.getApUserId());
            apAuthor.setCreatedTime(new Date());
            apAuthor.setType(ApAuthor.Type.PLATFORM_MEDIA.getValue());
            apAuthor.setName(wmUser.getName());
            kafkaTemplate.send(GlobalConstant.ADD_AP_AUTHOR,JSON.toJSONString(apAuthor));
        }
    }
}
